# Map Visualizations: Geographic Patterns
# Top: ChatGPT Search Intensity
# Bottom: 2x2 grid with all predictor variables
# Install plotly if needed
import sys
import subprocess
try:
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
except ImportError:
print("Installing plotly...")
subprocess.check_call([sys.executable, "-m", "pip", "install", "plotly"])
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
print("Plotly installed successfully!")
# Add state abbreviations for mapping
state_abbrev = {
'Alabama': 'AL', 'Alaska': 'AK', 'Arizona': 'AZ', 'Arkansas': 'AR', 'California': 'CA',
'Colorado': 'CO', 'Connecticut': 'CT', 'Delaware': 'DE', 'Florida': 'FL', 'Georgia': 'GA',
'Hawaii': 'HI', 'Idaho': 'ID', 'Illinois': 'IL', 'Indiana': 'IN', 'Iowa': 'IA',
'Kansas': 'KS', 'Kentucky': 'KY', 'Louisiana': 'LA', 'Maine': 'ME', 'Maryland': 'MD',
'Massachusetts': 'MA', 'Michigan': 'MI', 'Minnesota': 'MN', 'Mississippi': 'MS', 'Missouri': 'MO',
'Montana': 'MT', 'Nebraska': 'NE', 'Nevada': 'NV', 'New Hampshire': 'NH', 'New Jersey': 'NJ',
'New Mexico': 'NM', 'New York': 'NY', 'North Carolina': 'NC', 'North Dakota': 'ND', 'Ohio': 'OH',
'Oklahoma': 'OK', 'Oregon': 'OR', 'Pennsylvania': 'PA', 'Rhode Island': 'RI', 'South Carolina': 'SC',
'South Dakota': 'SD', 'Tennessee': 'TN', 'Texas': 'TX', 'Utah': 'UT', 'Vermont': 'VT',
'Virginia': 'VA', 'Washington': 'WA', 'West Virginia': 'WV', 'Wisconsin': 'WI', 'Wyoming': 'WY',
'District of Columbia': 'DC'
}
# Add state codes to merged_df if not already present
if 'state_code' not in merged_df.columns:
merged_df['state_code'] = merged_df['State'].map(state_abbrev)
# 1. TOP MAP: ChatGPT Search Intensity
print("=" * 80)
print("MAP 1: ChatGPT Search Intensity")
print("=" * 80)
fig_intensity = px.choropleth(
merged_df,
locations='state_code',
locationmode='USA-states',
color='Avg_AI_Interest',
scope='usa',
color_continuous_scale='Viridis',
title='ChatGPT Search Intensity by State',
labels={'Avg_AI_Interest': 'Search Intensity'},
hover_data=['State', 'Avg_AI_Interest']
)
fig_intensity.update_layout(
height=500,
title_x=0.5,
title_font_size=16,
font=dict(size=12)
)
fig_intensity.update_coloraxes(colorbar=dict(
thickness=8,
len=0.5,
x=1.01
))
fig_intensity.show()
print("\n✓ ChatGPT Search Intensity map created")
print("\n" + "=" * 80)
print("MAP 2: Median Household Income")
print("=" * 80)
# 2. Median Household Income
fig_income = px.choropleth(
merged_df,
locations='state_code',
locationmode='USA-states',
color='median_household_income',
scope='usa',
color_continuous_scale='Blues',
title='Median Household Income by State',
labels={'median_household_income': 'Income ($)'},
hover_data=['State', 'median_household_income']
)
fig_income.update_layout(
height=500,
title_x=0.5,
title_font_size=16,
font=dict(size=12)
)
fig_income.update_coloraxes(colorbar=dict(
thickness=8,
len=0.5,
x=1.01
))
fig_income.show()
print("\n✓ Median Household Income map created")
print("\n" + "=" * 80)
print("MAP 3: % Bachelor's Degree or Higher")
print("=" * 80)
# 3. Education % Bachelor's+
fig_education = px.choropleth(
merged_df,
locations='state_code',
locationmode='USA-states',
color='pct_bachelors_plus',
scope='usa',
color_continuous_scale='Greens',
title='% Adults with Bachelor\'s Degree or Higher by State',
labels={'pct_bachelors_plus': '% Bachelor\'s+'},
hover_data=['State', 'pct_bachelors_plus']
)
fig_education.update_layout(
height=500,
title_x=0.5,
title_font_size=16,
font=dict(size=12)
)
fig_education.update_coloraxes(colorbar=dict(
thickness=8,
len=0.5,
x=1.01
))
fig_education.show()
print("\n✓ Education map created")
print("\n" + "=" * 80)
print("MAP 4: % Households with Internet Access")
print("=" * 80)
# 4. Internet Access
fig_internet = px.choropleth(
merged_df,
locations='state_code',
locationmode='USA-states',
color='pct_with_internet',
scope='usa',
color_continuous_scale='Oranges',
title='% Households with Internet Access by State',
labels={'pct_with_internet': '% With Internet'},
hover_data=['State', 'pct_with_internet']
)
fig_internet.update_layout(
height=500,
title_x=0.5,
title_font_size=16,
font=dict(size=12)
)
fig_internet.update_coloraxes(colorbar=dict(
thickness=8,
len=0.5,
x=1.01
))
fig_internet.show()
print("\n✓ Internet Access map created")
print("\n" + "=" * 80)
print("MAP 5: % Knowledge Economy Employment")
print("=" * 80)
# 5. Knowledge Economy
fig_knowledge = px.choropleth(
merged_df,
locations='state_code',
locationmode='USA-states',
color='pct_knowledge',
scope='usa',
color_continuous_scale='Purples',
title='% Knowledge Economy Employment by State',
labels={'pct_knowledge': '% Knowledge Economy'},
hover_data=['State', 'pct_knowledge']
)
fig_knowledge.update_layout(
height=500,
title_x=0.5,
title_font_size=16,
font=dict(size=12)
)
fig_knowledge.update_coloraxes(colorbar=dict(
thickness=8,
len=0.5,
x=1.01
))
fig_knowledge.show()
print("\n✓ Knowledge Economy map created")
print("\n✓ All predictor variable maps created")
print("=" * 80)
print("\n" + "=" * 80)
print("MAP 6: Red/Blue States (2020 Presidential Election)")
print("=" * 80)
# 6. Red/Blue States Map
# Create a discrete color map: 0 = Red (Republican), 1 = Blue (Democratic)
# Convert blue_state to categorical string for proper discrete mapping
merged_df_temp = merged_df.copy()
merged_df_temp['political_affiliation'] = merged_df_temp['blue_state'].map({0: 'Republican', 1: 'Democratic'})
fig_political = px.choropleth(
merged_df_temp,
locations='state_code',
locationmode='USA-states',
color='political_affiliation',
scope='usa',
color_discrete_map={'Republican': '#E81B23', 'Democratic': '#0044C9'}, # Red for Republican, Blue for Democratic
title='Red States vs Blue States (2020 Presidential Election)',
labels={'political_affiliation': 'Political Affiliation'},
hover_data=['State', 'political_affiliation']
)
fig_political.update_layout(
height=500,
title_x=0.5,
title_font_size=16,
font=dict(size=12),
showlegend=True
)
# Update the traces for better visualization and remove colorbar
fig_political.update_traces(
marker_line_width=0.5,
marker_line_color='white',
showscale=False # Remove colorbar
)
fig_political.show()
print("\n✓ Red/Blue States map created")